package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.*;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**********************
 *
 *@Author 闫星忠
 *@Time 2021/12/16  16:10
 *
 */
@Service
public class InStockServiceImpl implements InStockService {



    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private InStockInfoMapper inStockInfoMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private ProductStockMapper productStockMapper;


//    分页展示物资入库信息列表
    @Override
    public PageVO<InStockVO> findInStockList(Integer pageNum, Integer pageSize, InStockVO inStockVO) {
        PageHelper.startPage(pageNum, pageSize);
//        Example o = new Example(InStock.class);
//        Example.Criteria criteria=o.createCriteria();
//        criteria.andEqualTo("status",inStockVO.getStatus());
//        if (!StringUtils.isEmpty(inStockVO.getInNum())) {
//            criteria.andEqualTo("inNum",  inStockVO.getInNum() );
//        }
//
//        if (!StringUtils.isEmpty(inStockVO.getType())) {
//            criteria.andEqualTo("type",  inStockVO.getType() );
//        }
//        Date startTime = inStockVO.getStartTime();
//        Date endTime = inStockVO.getEndTime();
//
//        if (startTime !=null&& endTime !=null) {
//            criteria.andBetween("createTime",startTime,endTime  );
//        }
//
//        List<InStock> inStocks = inStockMapper.selectByExample(o);

        List<InStockVO> inStockVOS = inStockMapper.findInStockList(inStockVO);
        //转vo
        PageInfo<InStockVO> info = new PageInfo<>(inStockVOS);
        return new PageVO<>(info.getTotal(), inStockVOS);
    }
//    点击明细按钮显示明细
    @Override
    public InStockDetailVO detail(Integer id, Integer pageNum) {
        InStockDetailVO inStockDetailVO = inStockMapper.detail(id);
        inStockDetailVO.setTotal(inStockDetailVO.getItemVOS().size());
        return inStockDetailVO;
    }


//    已入库记录移入回收站
    @Override
    public void remove(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(1);
        inStockMapper.updateByPrimaryKey(inStock);
    }

//    移出回收站到已入库
    @Override
    public void back(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
    }

//    从回收站删除入库记录
    @Override
    public void delete(Long id) {
        inStockMapper.deleteByPrimaryKey(id);
    }
//    商品入库
    @Override
    public void addIntoStock(InStockVO inStockVO) {
        Supplier supplier =null;
        if (inStockVO.getSupplierId() == null) {
            supplier = new Supplier();
            BeanUtils.copyProperties(inStockVO,supplier);
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            supplierMapper.insert(supplier);
        }
        InStock inStock = new InStock();
        BeanUtils.copyProperties(inStockVO,inStock);
        inStock.setCreateTime(new Date());
        inStock.setModified(new Date());
        inStock.setStatus(2);
        inStock.setOperator("admin");
        inStock.setSupplierId(supplier.getId());
        String inNum = UUID.randomUUID().toString().substring(0,32).replaceAll("-","");
        inStock.setInNum(inNum);
        Integer totalProductNumber=null;
        List<Object> products = inStockVO.getProducts();
        for (Object product : products) {

            Map<String,Object> map= (Map<String, Object>) product;
            Integer productId = (Integer) map.get("productId");
            Integer productNumber = (Integer) map.get("productNumber");
            totalProductNumber+=productNumber;
            Product productByProductId = productMapper.selectByPrimaryKey(productId);
            String pNum = productByProductId.getPNum();
            InStockInfo inStockInfo = new InStockInfo();
            inStockInfo.setInNum(inNum);
            inStockInfo.setPNum(pNum);
            inStockInfo.setProductNumber(productNumber);
            inStockInfo.setCreateTime(new Date());
            inStockInfo.setModifiedTime(new Date());
            inStockInfoMapper.insert(inStockInfo);

        }
        inStock.setProductNumber(totalProductNumber);
        inStockMapper.insert(inStock);
    }
//    待审核转到正常
    @Override
    public void publish(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
        List<ProductStock> productStocks = inStockMapper.selectStockInfo(id);
        if (!CollectionUtils.isEmpty(productStocks)){
            for (ProductStock productStock : productStocks) {
                String pNum = productStock.getPNum();
                Example example = new Example(ProductStock.class);
                example.createCriteria().andEqualTo("pNum",pNum);
                ProductStock o = (ProductStock) productStockMapper.selectOneByExample(example);
                if (o == null) {
                    ProductStock ps = new ProductStock();
                    ps.setPNum(pNum);
                    ps.setStock(productStock.getStock());
                    productStockMapper.insert(ps);
                }else {
                    ProductStock ps = new ProductStock();
                    ps.setId(o.getId());
                    ps.setPNum(pNum);
                    ps.setStock(productStock.getStock()+o.getStock());
                    productStockMapper.updateByPrimaryKey(ps);
                }

            }
        }
    }
}
